package com.github.soiff.shadow.plugin.utils;

import com.github.soiff.shadow.business.User;
import com.google.common.base.Defaults;
import com.google.common.collect.Maps;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by Soiff on 2016/5/22.
 */
public class ParamConverter {

    public static Object convertParam(String paramName, Class<?> paramClass, Map<String, Object> params, boolean isOptional) {
        if(User.class.isAssignableFrom(paramClass)) {
            //TODO: user validation
            return null;
        } else if(HttpServletRequest.class.isAssignableFrom(paramClass)) {
            //TODO: inject request object.
            return null;
        } else if(HttpServletResponse.class.isAssignableFrom(paramClass)) {
            //TODO: inject response object.
            return null;
        } else {
            Object param = params.get(paramName);
            return param == null && Map.class.isAssignableFrom(paramClass)?filterCustomObject(params):(!paramClass.isPrimitive()?param:(param == null? Defaults.defaultValue(paramClass):param));
        }
    }

    private static Map<String, String> filterCustomObject(Map<String, Object> params) {
        HashMap targetParam = Maps.newHashMapWithExpectedSize(params.size());
        Iterator var2 = params.keySet().iterator();

        while(var2.hasNext()) {
            String key = (String)var2.next();
            Object value = params.get(key);
            if(value != null && ParamUtil.isBaseClass(value)) {
                targetParam.put(key, String.valueOf(value));
            }
        }

        return targetParam;
    }

    public interface UnKnowClass {
    }
}
